<?php
/**
 * Created by PhpStorm.
 * User: Kyle
 * Date: 2019/9/5
 * Time: 11:41
 */

namespace app\logic;


use app\common\model\Topic;
use app\common\model\User;
use think\Exception;

class TopicLogic
{
    public function validateTopic($data){
        if(empty($data['type'])){wapAjaxReturn(400,'请求数据不对');}
        if(!in_array($data['type'],[1,2])){wapAjaxReturn(400,'请求数据不对');}
        if($data['type'] == 2){
            if(empty($data['id'])|| !is_numeric($data['id']+0)||$data['id']<=0){wapAjaxReturn(400,'请求数据不对');}
        }
        if(empty($data['title'])){wapAjaxReturn(400,'请填写标题');}
        if(mb_strlen($data['title']) > 80){wapAjaxReturn(400,'标题不能超过80个字');}
        if(!empty($data['description'])){
            if(mb_strlen($data['description']) > 200){wapAjaxReturn(400,'描述不能超过200个字');}
        }
        if(empty($data['banner_img'])){wapAjaxReturn(400,'请上传封面图');}
//        if(empty($data['topic_type'])){wapAjaxReturn(400,'请填写题目');}
//        foreach($data['topic_type'] as $k=>$v){
//            if(empty($data['topic_question'][$k])){wapAjaxReturn(400,'第'.($k+1).'题的题目请填写');}
//            if(!in_array($v,[2,3])){
//                if(empty($data['topic_answer'][$k])){wapAjaxReturn(400,'第'.($k+1).'题的答案请填写');}
//            }
//        }
//        if(empty($data['sucai_img'])){wapAjaxReturn(400,'请上传素材图片');}
//        if(empty($data['sucai_img_origin'])){wapAjaxReturn(400,'请上传素材图片');}
        if(!empty($data['share_content'])){
            if(mb_strlen($data['share_content']) > 200){wapAjaxReturn(400,'分享内容不能超过200个字');}
        }
        if(!empty($data['share_shuoshuo'])){
            if(mb_strlen($data['share_shuoshuo']) > 200){wapAjaxReturn(400,'分享说说不能超过200个字');}
        }
        if(empty($data['img_proportion'])){
            $data['img_proportion'] = 0;
        }

        if($data['tt_need_share'] == 2){
            //头条测评使用广告分销佣金，判断费用是否正确
            if(!empty($data['ad_self'])){
                if(!is_numeric($data['ad_self'])){wapAjaxReturn(400,'广告佣金费用只能是数字');}
                if($data['ad_self']<0 || $data['ad_self'] > 10){wapAjaxReturn(400,'广告佣金费用在0-10之间');}
                if(getPointLen($data['ad_self']) > 2){wapAjaxReturn(400,'广告佣金费用小数点最多2位');}

            }
            if(!empty($data['ad_fir_user'])){
                if(!is_numeric($data['ad_fir_user'])){wapAjaxReturn(400,'广告佣金费用只能是数字');}
                if($data['ad_fir_user']<0 || $data['ad_fir_user'] > 10){wapAjaxReturn(400,'广告佣金费用在0-10之间');}
                if(getPointLen($data['ad_fir_user']) > 2){wapAjaxReturn(400,'广告佣金费用小数点最多2位');}

            }
            if(!empty($data['ad_sec_user'])){
                if(!is_numeric($data['ad_sec_user'])){wapAjaxReturn(400,'广告佣金费用只能是数字');}
                if($data['ad_sec_user']<0 || $data['ad_sec_user'] > 10){wapAjaxReturn(400,'广告佣金费用在0-10之间');}
                if(getPointLen($data['ad_sec_user']) > 2){wapAjaxReturn(400,'广告佣金费用小数点最多2位');}

            }

        }
        if($data['tt_need_share'] == 3){
            //头条测评使用支付，判断支付费用是否正确
            if(!isset($data['tt_play_price'])){wapAjaxReturn(400,'请填写支付费用');}
            if(!is_numeric($data['tt_play_price'])){wapAjaxReturn(400,'支付费用只能是数字');}
            if($data['tt_play_price']<0 || $data['tt_play_price'] > 10){wapAjaxReturn(400,'支付费用在0-10之间');}
            if(getPointLen($data['tt_play_price']) > 2){wapAjaxReturn(400,'支付费用小数点最多2位');}

            if(!empty($data['fir_distribution'])){
                if(!is_int($data['fir_distribution']+0)){wapAjaxReturn(400,'一级分销比例只能是正整数百分比');}
                if($data['fir_distribution'] <0 || $data['fir_distribution'] >100){
                    wapAjaxReturn(400,'二级分销比例不能小于0且不能大于100');
                }
            }
            if(!empty($data['sec_distribution'])){
                if(!is_int($data['sec_distribution']+0)){wapAjaxReturn(400,'二级分销比例只能是正整数百分比');}
                if($data['sec_distribution'] <0 || $data['sec_distribution'] >100){
                    wapAjaxReturn(400,'二级分销比例不能小于0且不能大于100');
                }
            }
        }
    }

    public function save($data){
        try{
            //组装存储数据库的字段
            $topic['title'] = $data['title'];
            $topic['description'] = $data['description'];
            $topic['category'] = $data['category'];
            $topic['banner_img'] = $data['banner_img'];
//            $topic['topic'] = json_encode([
//                'topic_type'=>$data['topic_type'],'topic_question'=>$data['topic_question'],'topic_question_img'=>$data['topic_question_img'],'topic_answer'=>$data['topic_answer']
//            ]);
//            $topic['result_type'] = $data['result_type'];
//            $topic['result_img'] = json_encode([
//                'sucai_img'=>$data['sucai_img'],'sucai_img_origin'=>$data['sucai_img_origin'],'img_proportion'=>$data['img_proportion']
//            ]);
//            $topic['nickname_attr'] = json_encode($data['nickname']);
//            $topic['headerimg_attr'] = json_encode($data['headerimg']);
//            $topic['qrcode_attr'] = json_encode($data['qrcode_attr']);
            $topic['need_share'] = $data['need_share'];
            $topic['weixin_need_share'] = $data['weixin_need_share'];
            $topic['tt_need_share'] = $data['tt_need_share'];
            $topic['share_content'] = $data['share_content'];
            $topic['share_shuoshuo'] = $data['share_shuoshuo'];
            $topic['share_img'] = $data['share_img'];
            $topic['weight'] = $data['weight'];

            if($data['tt_need_share'] == 3){
                $topic['tt_play_price'] = !empty($data['tt_play_price'])?$data['tt_play_price']:0;
                $topic['fir_distribution'] = !empty($data['fir_distribution'])?$data['fir_distribution']:0;
                $topic['sec_distribution'] = !empty($data['sec_distribution'])?$data['sec_distribution']:0;
            }else{
                $topic['tt_play_price'] = 0;
                $topic['fir_distribution'] = 0;
                $topic['sec_distribution'] = 0;
            }

            if($data['tt_need_share'] == 2) {
                $topic['ad_self'] = !empty($data['ad_self']) ? $data['ad_self'] : 0;
                $topic['ad_fir_user'] = !empty($data['ad_fir_user']) ? $data['ad_fir_user'] : 0;
                $topic['ad_sec_user'] = !empty($data['ad_sec_user']) ? $data['ad_sec_user'] : 0;
            }else{
                $topic['ad_self'] = 0;
                $topic['ad_fir_user'] = 0;
                $topic['ad_sec_user'] = 0;
            }


//            $topic['qq_qrcode'] = $data['qq_qrcode'];
//            $topic['weixin_qrcode'] = $data['weixin_qrcode'];
//            $topic['tt_qrcode'] = $data['tt_qrcode'];

            if(1 == $data['type']){
                $topic['base_game_count'] = rand(1000,8000);
            }

            $topic['tt_share_template_id'] = $data['tt_share_template_id'];
//            print_r($topic);exit;
            $user = new Topic();
            // 过滤post数组中的非数据表字段数据
            switch ($data['type']){
                case 1:
                    $topic['created_at'] = time();
                    $topic['updated_at'] = time();
                    $save = $user->allowField(true)->save($topic);
                    $save ? wapAjaxReturn(0,'新增成功',[],'/admin/topic') : wapAjaxReturn(400,'新增失败');
                    break;
                case 2:
                    $topic['updated_at'] = time();
                    $save = $user->allowField(true)->save($topic,['topic_id'=>$data['id']]);
                    $save ? wapAjaxReturn(0,'更新成功',[],'/admin/topic') : wapAjaxReturn(400,'更新失败');
                    break;
            }

        }catch (Exception $e){
            wapAjaxReturn(400,'系统异常',$e->getMessage());
        }
    }

    public function validateImgWithTopic(&$params,&$topicInfo,&$userInfo){
        if(empty($params['topic_id']) || !is_numeric($params['topic_id']) || $params['topic_id'] < 0 ){wapAjaxReturn(400,'缺少id');}
        $topicInfo = Topic::with(['questions','answers'])->where('topic_id',$params['topic_id'])->find()->toArray();
        if(empty($topicInfo)){wapAjaxReturn(400,'数据不存在');}
        if(empty($params['openid'])){wapAjaxReturn(500,'缺少openid');}
        $userInfo = User::where('openid',$params['openid'])->find();
        if(empty($userInfo)){
            $saveUser = (new UserLogic())->saveOpenid($params['openid'],$params['platform'],$params['parentOpenid']);
            if($saveUser['userId'] == 0){
                wapAjaxReturn(500,'用户信息不存在');
            }else{
                $userInfo['user_id'] = $saveUser['userId'];
            }
        }
        if(empty($params['result'])){wapAjaxReturn(400,'缺少测试结果!');}
        $params['result_arr'] = json_decode($params['result'],true);
        if(empty($params['result_arr'])){wapAjaxReturn(400,'缺少测试结果');}
        $topicInfo['topic'] = json_decode($topicInfo['topic'],true);
        unset($params['result']);
        $topicInfo['result_img'] = json_decode($topicInfo['result_img'],true);

    }

    /**
     * @param $params
     * @param $topicInfo
     * 随机返回测评的结果背景图
     */
    public function getImgWithTopic($params,$topicInfo){
        //获取每个题目的选项对应的分数
        $score = 0;
        $img = '';
        if(!empty($topicInfo['answers'])){
            foreach ($params['result_arr'] as $k=>$v){
                $options = json_decode($topicInfo['questions'][$k]['question_options'],true);
                foreach ($options['content'] as $k2=>$v2){
                    if($v == $v2){$score += $options['score'][$k2];break;}
                }

            }

            //通过答案的区间获取，如果没有对应分数的则取第一个答案
            foreach ($topicInfo['answers'] as $v){
                if($score >= $v['min_score'] && $score <= $v['max_score']){
                    $img = $v['answer_img'];break;
                }
            }
            if(empty($img)){$img = $topicInfo['answers'][0]['answer_img'];}
        }
        $result = ['img'=>getThumbImg($img,'')];
        wapAjaxReturn(0,'查询成功',$result);
    }
}